home *** CD-ROM | disk | FTP | other *** search
- #include "SpriteTools.h"
-
-
- Boolean gDone = false;
- short TheMenuItem;
- Boolean killed = false;
-
- static void InitSpriteEngine(Rect *offBounds, short backgroundPictureID)
- {
- PicHandle backgroundPicture;
- GDHandle saveGD;
- GWorldPtr savePort;
-
- MyNewGWorld(&gOffScreen, offBounds);
- MyNewGWorld(&gBackScreen, offBounds);
-
- GetGWorld(&savePort, &saveGD);
- SetGWorld((GWorldPtr)gBackScreen, nil);
-
- EraseRect(offBounds);
-
- backgroundPicture = GetPicture(backgroundPictureID);
- if (backgroundPicture != nil)
- {
- DrawPicture(backgroundPicture, offBounds);
- ReleaseResource((Handle)backgroundPicture);
- }
- SetGWorld((GWorldPtr)gOffScreen, nil);
- CopyBits(&gBackScreen->portBits, &gOffScreen->portBits, offBounds, offBounds, srcCopy, nil);
- SetGWorld((GWorldPtr)myWindow, GetMainDevice());
- CopyBits(&gOffScreen->portBits, &myWindow->portBits, offBounds, offBounds, srcCopy, nil);
- SetGWorld(savePort, saveGD);
- }
-
-
- static void RunSpriteEngine()
- {
- #define kWallBounce 7
- #define kBallDiameterSquared (32*32)
-
- Rect tmpRect;
- GDHandle saveGD;
- GWorldPtr savePort;
- SpritePtr theSprite, anotherSprite;
- Rect bounds1, bounds2;
-
- GetGWorld(&savePort, &saveGD);
-
- SetGWorld((GWorldPtr)gOffScreen, nil);
-
- theSprite = gSpriteList;
- while (theSprite != nil)
- {
- theSprite->drawingRect = theSprite->face->portRect;
- OffsetRect(&theSprite->drawingRect, theSprite->position.h, theSprite->position.v);
- CopyBits(&gBackScreen->portBits, &gOffScreen->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
-
- theSprite = theSprite->next;
- }
-
- theSprite = gSpriteList;
- while (theSprite != nil)
- {
- MoveSprite(theSprite);
-
- theSprite = theSprite->next;
- }
-
- theSprite = gSpriteList;
- while (theSprite != nil)
- {
- bounds1 = theSprite->face->portRect;
- OffsetRect(&bounds1, theSprite->position.h, theSprite->position.v);
-
- anotherSprite = theSprite->next;
- while (anotherSprite != nil)
- {
- bounds2 = anotherSprite->face->portRect;
- OffsetRect(&bounds2, anotherSprite->position.h, anotherSprite->position.v);
-
- if (SectRect(&bounds1, &bounds2, &tmpRect))
- HitSprite(theSprite, anotherSprite);
-
- anotherSprite = anotherSprite->next;
- }
- theSprite = theSprite->next;
- }
-
- theSprite = gSpriteList;
- while (theSprite != nil)
- {
- tmpRect = theSprite->face->portRect;
- OffsetRect(&tmpRect, theSprite->position.h, theSprite->position.v);
- PlotFace(theSprite->face, gOffScreen, *(Point *)&tmpRect);
-
- theSprite = theSprite->next;
- }
-
- SetGWorld((GWorldPtr)myWindow, saveGD);
- theSprite = gSpriteList;
- while (theSprite != nil)
- {
- CopyBits(&gOffScreen->portBits, &myWindow->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
- theSprite->drawingRect = theSprite->face->portRect;
- OffsetRect(&theSprite->drawingRect, theSprite->position.h, theSprite->position.v);
- CopyBits(&gOffScreen->portBits, &myWindow->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
-
- theSprite = theSprite->next;
- }
- SetGWorld(savePort, saveGD);
- };
-
-
-
- static void InitToolbox(void) {
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent,0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (nil);
- InitCursor ();
- }
-
-
-
- static void InitStuff()
- {
- Rect windowRectangle;
-
- SetRect(&windowRectangle, 50, 50, 512, 342);
- myWindow = NewCWindow(nil, &windowRectangle, "\pRurl", true, documentProc, (WindowPtr)-1L, false, 0);
- SetPort(myWindow);
-
- qd.randSeed = TickCount ();
- }
-
-
- #define kBackgroundPictureID 128
-
-
- void main(void)
- {
- InitToolbox();
- Initialize();
- EventLoop();
- ShowCursor();
-
- FlushEvents(mDownMask, 0);
- }
-
- void startGame(void)
- {
- long startTime;
- initMySprites();
- while (!killed)
- {
- startTime = TickCount();
- RunSpriteEngine();
- while (TickCount() < startTime + kFrameTime);
- }
- }
-
- void initMySprites(void)
- {
- InitStuff();
- InitSpriteEngine(&myWindow->portRect, kBackgroundPictureID);
- InitSprites();
- }
-
- void EventLoop()
- {
- EventRecord theEvent;
- short thePart;
- BitMap screenBits;
- WindowPtr myWindow;
- long WhatMenu;
- short TheMenu;
-
- while(gDone != true)
- {
- WaitNextEvent( everyEvent, &theEvent, 0L, 0L );
- switch( theEvent.what )
- {
- case mouseDown:
- thePart = FindWindow( theEvent.where, &myWindow );
- switch( thePart )
- {
- case inDrag:
- if ( (myWindow != FrontWindow ()) && ((theEvent.modifiers & cmdKey) == 0) )
- SelectWindow(myWindow);
- DragWindow(myWindow, theEvent.where, &qd.screenBits.bounds);
- break;
-
- case inMenuBar:
- WhatMenu = MenuSelect( theEvent.where );
- if( WhatMenu != 0)
- {
- TheMenu = HiWord( WhatMenu );
- TheMenuItem = LoWord( WhatMenu );
- switch( TheMenu )
- {
- case 128:
- DoAppleMenu();
- break;
-
- case 129:
- DoFileMenu();
- break;
- }
- }
- HiliteMenu(0);
- break;
- }
- break;
-
- case keyDown:
- case autoKey:
- CheckKey(&theEvent);
- break;
- }
- }
- }
-
- void DoAppleMenu()
- {
- switch( TheMenuItem )
- {
- case 1:
- SysBeep(0);
- break;
- }
- }
-
- void Quit()
- {
- gDone = true;
- }
-
- void CheckKey(EventRecord *theEvent)
- {
- char theChar;
- Boolean commandDown;
-
- theChar = theEvent->message & charCodeMask;
- commandDown = ((theEvent->modifiers & cmdKey) != 0);
-
- if (commandDown)
- {
- switch(theChar)
- {
- case 'q':
- Quit();
- break;
-
- case 'n':
- startGame();
- break;
- }
- }
- }
-
- void DoFileMenu()
- {
- switch( TheMenuItem )
- {
- case 1:
- startGame();
- break;
-
- case 3:
- Quit();
- break;
- }
- }
-
-
- void Initialize()
- {
- Handle MenuBarHandle;
- WindowPtr myWindow;
-
- MenuBarHandle = GetNewMBar( 128 );
- SetMenuBar( MenuBarHandle );
- DrawMenuBar();
- }